package org.ch02.utils;

import java.lang.reflect.Field;

/**
 * @author lwj
 * @date 2025-03-18
 * @vesion 1.0.0
 */
public class SqlGenerate {
    public static String generateSelect(Class<?> clz){
        StringBuffer sql = new StringBuffer("select ");
        try{
            // 获取字段信息
            Field[] fields = clz.getDeclaredFields();
            // 遍历字段信息
            for (int i = 0; i < fields.length; i++) {
                sql.append(fields[i].getName());
                // 最后一个不加 ,
                if(i < fields.length - 1){
                    sql.append(",");
                }
            }
            // 获取类名
            String className = clz.getSimpleName();

            StringBuffer sb = new StringBuffer();
            sb.append(Character.toLowerCase(className.charAt(0)));// 取出第一个字符,并转成小写
            for(int i = 1; i < className.length(); i++){
                char c = className.charAt(i);
                // 字符为大写
                if(Character.isUpperCase(c)){
                    sb.append("_"+Character.toLowerCase(c));
                }else{
                    sb.append(c);
                }
            }

            sql.append(" from "+sb);
        }catch(Exception e){
            throw new RuntimeException(e);
        }
        return sql.toString();
    }
}
